package repository;

import core.JitInit;
import gitobject.Head;

import java.io.File;
import java.io.IOException;

/**
 * Todo: Add your own code. JitInit.init("worktree") should be able to create a repository "worktree/.jit" ,
 *       which contains all the default files and other repositories inside. If the repository has
 *       already exists, delete the former one and create a new one. You're welcome to reconstruct the code,
 *       but don't change the code in the core directory.
 */
//在worktree上建立一个文件夹gitDir
public class Repository {
    private static String workTree;	//working工作目录
    private static String gitDir;	//jit repository path仓库路径
    /**
     * Constructor
     */
    public Repository() throws IOException {
        if(gitDir == ""){
            throw new IOException("The repository does not exist!");
        }
    }
    
    /**
     * Construct a new repository instance with certain path.
     * Constructor
     * @param path
     * @throws IOException
     */
    //入参为工作区路径
    public Repository(String path) throws IOException {
        this.workTree = path;
        this.gitDir = path + File.separator + ".jit";//生成版本库路径为path/.jit
    }//worktree和gitDir都为文件路径

    public static String getGitDir() {
        return gitDir;
    } //静态函数 ，调用方式Repository.

    public static String getWorkTree() { return workTree; }
    
    /**
     * Helper functions.
     * @return
     */
    public boolean exist(){ return new File(gitDir).exists(); }

    public boolean isFile(){ return new File(gitDir).isFile(); }

    public boolean isDirectory(){ return new File(gitDir).isDirectory(); }


    /**
     * Create the repository and files and directories inside.
     * @return boolean
     * @throws IOException
     */
    //在worktree上建立一个文件夹gitDir
    public void createRepo() throws IOException {
    /* 此处省略Path的判断条件 */
        File file = new File(this.gitDir);
 //       File objects = new File(Repository.getGitDir() + File.separator + "objects" );
 //       File refs = new File(Repository.getGitDir() + File.separator + "refs" );
 //       File heads = new File(Repository.getGitDir() + File.separator + "refs" + File.separator + "heads");
 //       File index = new File(Repository.getGitDir() + File.separator + "index" );
 //       File HEAD = new File(Repository.getGitDir() + File.separator + "HEAD" );

        try{
            file.mkdirs();//io.File库函数
//            refs.mkdirs();
//            objects.mkdirs();
//            heads.mkdirs();
//            HEAD.createNewFile();
//            index.createNewFile();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

/*    public static void main(String[] args) {
        try{
            Repository repo = new Repository("C:/Users/之靈/Desktop/Git");
            System.out.println(Repository.getGitDir());
            System.out.println(Repository.getWorkTree());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
